<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input v-model="queryParams.contactName" placeholder="请输入联系人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="启用" prop="valid">
        <el-select v-model="queryParams.valid" placeholder="请选择启用" clearable>
          <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreatedDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['disposal:disposalLand:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['disposal:disposalLand:edit']">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['disposal:disposalLand:remove']">批量删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['disposal:disposalLand:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="disposalLandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="name" width="200" show-overflow-tooltip />
      <el-table-column label="投产日期" align="center" prop="putProductionData" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.putProductionData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center" prop="valid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_valid" :value="scope.row.valid" />
        </template>
      </el-table-column>
      <el-table-column label="企业地址" align="center" prop="address" width="100" show-overflow-tooltip />
      <el-table-column label="联系人姓名" align="center" prop="contactName" />
      <el-table-column label="手机号" align="center" prop="contactPhone" width="110" />
      <el-table-column label="处置能力" align="center" prop="disposalAbility" />
      <el-table-column label="定位" align="center" prop="location">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLocation(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="120" show-overflow-tooltip />
      <el-table-column label="地磅编号" align="center" prop="weighbridgeNo" width="120" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createdDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后数据上报时间" align="center" prop="finalDataReportTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finalDataReportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['disposal:disposalLand:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['disposal:disposalLand:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改处置地对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置能力" prop="disposalAbility">
              <el-input v-model="form.disposalAbility" placeholder="请输入处置能力" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投产日期" prop="putProductionData">
              <el-date-picker clearable v-model="form.putProductionData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择投产日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用">
              <el-switch v-model="form.valid" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人姓名" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入联系人姓名" maxlength="10" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入手机号" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地磅编号" prop="weighbridgeNo">
          <el-input v-model="form.weighbridgeNo" placeholder="请输入地磅编号" maxlength="50" />
        </el-form-item>
        <el-form-item label="资源利用化企业地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" maxlength="50" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200" />
        </el-form-item>
        <el-form-item label="项目图片" prop="images">
          <image-upload v-model="form.images" :limit="5" />
        </el-form-item>
        <TFMap :longitude="longitude" :latitude="latitude" :currentAddress="address" @click="updateLocation"
          tid="map_disposalLand" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--地图显示-->
    <el-dialog :visible.sync="mapOpen" fullscreen append-to-body>
      <TMap :longitude="longitude" :latitude="latitude" :address="address" tid="tmap_disposalLand" />
    </el-dialog>
  </div>
</template>

<script>
import { listDisposalLand, getDisposalLand, delDisposalLand, addDisposalLand, updateDisposalLand } from "@/api/disposal/disposalLand";
import TMap from '@/components/Tmap/TMap';
import TFMap from '@/components/Tmap/TFMap';

export default {
  name: "DisposalLand",
  dicts: ['sys_valid'],
  components: { TMap, TFMap },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处置地表格数据
      disposalLandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 最后数据上报时间时间范围
      daterangePutProductionData: [],
      // 最后数据上报时间时间范围
      daterangeCreatedDate: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: null,
        contactName: null,
        contactPhone: null,
        putProductionData: null,
        valid: null,
        address: null,
        createdDate: null,
        finalDataReportTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        contactName: [
          { required: true, message: "联系人姓名不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: this.$ConstantUtil.REG_PHONE,
            message: "请输入正确的手机号码",
            trigger: ["blur", "change"]
          }
        ],
      },
      mapOpen: false,
      // 定位
      position: {},
      longitude: 118.83414506997173,
      latitude: 31.954718562357378,
      address: '江宁区政府'
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询处置地列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePutProductionData && '' != this.daterangePutProductionData) {
        this.queryParams.params["beginPutProductionData"] = this.daterangePutProductionData[0];
        this.queryParams.params["endPutProductionData"] = this.daterangePutProductionData[1];
      }
      if (null != this.daterangeCreatedDate && '' != this.daterangeCreatedDate) {
        this.queryParams.params["beginCreatedDate"] = this.daterangeCreatedDate[0];
        this.queryParams.params["endCreatedDate"] = this.daterangeCreatedDate[1];
      }
      listDisposalLand(this.queryParams).then(response => {
        this.disposalLandList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        name: null,
        disposalAbility: null,
        location: null,
        contactName: null,
        contactPhone: null,
        putProductionData: null,
        remark: null,
        valid: null,
        address: null,
        createdDate: null,
        modifiedDate: null,
        finalDataReportTime: null,
        images: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangePutProductionData = [];
      this.daterangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加处置地";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getDisposalLand(id).then(response => {
        this.form = response.data;
        const { location, address } = response.data;
        if (location) {
          this.longitude = location.coordinates[0];
          this.latitude = location.coordinates[1];
        }
        this.address = address;
        this.open = true;
        this.title = "修改处置地";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDisposalLand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDisposalLand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delDisposalLand(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('disposal/disposalLand/export', {
        ...this.queryParams
      }, `末端资源化利用企业管理_${new Date().getTime()}.xlsx`)
    },
    // 定位
    handleLocation (row) {
      this.longitude = row.location.coordinates[0];
      this.latitude = row.location.coordinates[1];
      this.address = row.address;
      this.mapOpen = true;
    },
    // 更新位置信息
    updateLocation (localtionInfo) {
      console.log(1234);
      this.form.address = localtionInfo.address;
      this.form.location = {
        "type": "Point",
        "coordinates": localtionInfo.coordinates
      }
    }
  }
}
</script>
